//
//  AccountSettingController.m
//  NPS
//
//  Created by jadd on 15/10/13.
//  Copyright © 2015年 jadd. All rights reserved.
//

#import "AccountSettingController.h"

@implementation AccountSettingController
@synthesize  array;
@synthesize URL;
@synthesize companyName;


-(void)logoutAction
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
                                                    message:@"确定退出登录吗？"
                                                   delegate:self
                                          cancelButtonTitle:@"取消"
                                          otherButtonTitles:@"确定",nil];
    alert.tag = 1;
    [alert show];
    
}


-(NSString *)getImage: (NSUInteger) index
{
    switch (index) {
        case 0:
            return @"Password";
            break;
        case 2:
            return @"About";
            break;
        case 1:
            return @"About";
            break;
        default:
            return @"";
            break;
    }
}

-(void)backACtion{
    
    [self dismissViewControllerAnimated:YES completion:^{
        NSLog(@"");
    }];
}

- (void)viewDidLoad {


    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    self.title = @"账户设置";

    self.companyName = [[NSUserDefaults standardUserDefaults] objectForKey:UserDefault_uName];
    NSMutableArray *ary = [[NSMutableArray alloc] initWithObjects:@"防作弊设置",@"修改密码",@"更换logo",@"关于我们", nil];
    self.array = ary;
    
    
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom] ;
    button.frame = CGRectMake(0, 0, K_Global_BackButton_Width, K_Global_BackButton_Height);
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [button setImage:[UIImage imageNamed:@"back_white_on"] forState:UIControlStateNormal];
    //[button setImage:[UIImage imageNamed:@"back_white_on"] forState:UIControlStateHighlighted];
    [button addTarget:self action:@selector(backACtion) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * leftButton = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.leftBarButtonItem = leftButton;
    
    
    [self.tableView setShowsVerticalScrollIndicator:NO];
    [super viewDidLoad];
    
}

-(void)viewWillAppear:(BOOL)animated
{
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.delegate = self;
    }
    

    [self.tableView reloadData];
    [super viewWillAppear:animated];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(section == 0)
    {
        return 1;
    }
    if(section == 1){
        return [self.array count];
    }
    if(section == 2){
        return 1;
    }
    return 0;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 0)
        return 75;
    else
        return 60;
    return 0;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}

-(CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    
    return 0;
}
/*
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    
    return 1;
    
}
 */



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
  
    static NSString *identifier = @"TJCell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    //if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    //}
    
    NSUInteger section = [indexPath section];

    if(section == 0)
    {
        CGRect rect = CGRectMake(15, 8, 60.0f, 60.0f);
        UIImageView *image = [[UIImageView alloc] initWithFrame:rect];
     
        [image setImage:[UIImage imageNamed:@"dianpu"]];
        [image setBackgroundColor:CLEAR];
        
        [cell.contentView addSubview:image];

        UILabel *lable = [[UILabel alloc] init];
        lable.frame = CGRectMake(95, 15, self.view.bounds.size.width - 100, 40.0f);
        lable.text = self.companyName;
        lable.font = [UIFont boldSystemFontOfSize:20];
        lable.backgroundColor = CLEAR;
        [cell.contentView addSubview:lable];
        
        
    }
    if(section == 1){
        NSUInteger row = [indexPath row];
        cell.textLabel.text = [self.array objectAtIndex:row];
        //UIImage *im = [UIImage imageNamed:[self getImage: row]];
        //cell.imageView.image = im;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    
    if(section == 2){
        UILabel *lable = [[UILabel alloc] init];
        lable.frame = CGRectMake((self.view.bounds.size.width/2  -100), 10,  200, 40.0f);
        lable.text = @"退出登录";
        lable.textAlignment = NSTextAlignmentCenter;
        lable.textColor = RED;
        lable.font = [UIFont systemFontOfSize:20];
        
        [cell.contentView addSubview:lable];
    }
    //cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}


#pragma mark - Table view delegate
-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
    
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSUInteger section = indexPath.section;
    NSUInteger row = indexPath.row;
    if(section == 1 && row == 0)
    {
        PreventCheatController *controller = [[PreventCheatController alloc] initWithStyle:UITableViewStyleGrouped];
        [self.navigationController pushViewController:controller animated:YES];
    }
    if(section == 1 && row == 1)
    {
        ModifyPwdController *controller = [[ModifyPwdController alloc] initWithStyle:UITableViewStyleGrouped];
        [self.navigationController pushViewController:controller animated:YES];
    }
    if(section == 1 && row == 2)
    {
        //ChangeLogoController *controller = [[ChangeLogoController alloc] initWithStyle:UITableViewStyleGrouped];
        
        UpdateLogoController *controller = [[UpdateLogoController alloc] init];
        controller.link = self.URL;
        [self.navigationController pushViewController:controller animated:YES];

    }
    if(section == 1 && row == 3)
    {
        AboutUsController *controller = [[AboutUsController alloc] init];
        [self.navigationController pushViewController:controller animated:YES];
    }
    if(section == 2 && row == 0)
    {
        [self logoutAction];
    }

}

#pragma marks -- UIAlertViewDelegate --
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(alertView.tag == 1 && buttonIndex == 1){
        
        [Config showActivityView];
        //退出登录
        
        [self backACtion];
        [MyGlobal sharedVariables].isReLogin = NO;
        UINavigationController *rootNavController = (UINavigationController *)[AppDelegate shareInstance].window.rootViewController;
        [rootNavController popToRootViewControllerAnimated:YES];
        
        LogoutRequest * request = [[LogoutRequest alloc] initWithUserId:[Config getUser_id]];
        [RequestHandle startOperationWithRequest:request
                                        complete:^(id responseObject) {
                                            
                                            LogoutResponse * response = [[LogoutResponse alloc] init];
                                            response.responseObject = responseObject;
                                        
                                            while (![[NSUserDefaults standardUserDefaults] synchronize]) {}
                                            //NSString *result = [responseObject objectForKey:@"status"];
                                            //if([result isEqualToString:@"success"])
                                            //{
                                            
                                            //}
                                            [Config hideActivityView];
                                            [Config logout];
                                            
                                        } error:^(NSError *error) {
                                            //NSLog(@"%@",error.localizedDescription);
                                            [Config hideActivityView];
                                            //[Config showMessage:error.localizedDescription];
                                            
                                        }];
        
    }
}

@end
